package com.example.devices

import android.content.Intent
import android.text.Editable
import androidx.activity.viewModels
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.base.app.path.RouterPath
import com.example.common.view.view.BaseMvvmActivity
import com.example.device.R
import com.example.device.databinding.ActivityPostDevicesBinding
import com.example.devices.entity.Devices
import com.example.devices.vm.DevicesViewModel
import com.example.image.glide.GlideImgLoader
import com.github.gzuliyujiang.wheelpicker.DatePicker
import com.github.gzuliyujiang.wheelpicker.OptionPicker
import com.github.gzuliyujiang.wheelpicker.entity.DateEntity
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = RouterPath.PATH_DEVICES_POST)
class PostDevicesActivity : BaseMvvmActivity<ActivityPostDevicesBinding>() {
    private val postDevicesViewModel:DevicesViewModel by viewModels()

    override fun initView() {
        mDataBinding.back.setOnClickListener {
            finish()
        }
        //类型选择器
        mDataBinding.moreType.setOnClickListener {
            val list = mutableListOf("门禁设备","监控设备","其它")
            val optionPicker = OptionPicker(this)
            optionPicker.setData(list)
            optionPicker.setTitle("选择工单类型")
            optionPicker.setOnOptionPickedListener { position, item ->
                mDataBinding.edType.text = Editable.Factory.getInstance().newEditable(list[position])
            }
            optionPicker.show()
        }
        //时间选择器
        mDataBinding.moreTime.setOnClickListener {
            val datePicker = DatePicker(this)
            datePicker.setTitle("请选择生日")
            datePicker.wheelLayout.setRange(DateEntity.target(1900,1,1), DateEntity.target(2600,1,1),DateEntity.today())
            datePicker.setOnDatePickedListener { year, month, day ->
                mDataBinding.edTime.text = Editable.Factory.getInstance().newEditable("$year-$month-$day")
            }
            datePicker.show()
        }
        //添加图片
        mDataBinding.ivAdd.setOnClickListener {
            PictureSelector.create(this,200).selectPicture()
        }
        mDataBinding.btnCommit.setOnClickListener {
            postDevicesViewModel.postDevices(Devices(mDataBinding.edType.text.toString(),mDataBinding.edType.text.toString(),0.2,0,mDataBinding.edAddress.text.toString(),mDataBinding.edContent.text.toString()))
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode == 200 && resultCode == RESULT_OK){
            val bean = data?.extras?.get(PictureSelector.PICTURE_RESULT) as PictureBean
            GlideImgLoader.loadImage(this,bean.path,mDataBinding.ivAdd)
        }
    }
    override fun observeDataChange() {
        super.observeDataChange()
        postDevicesViewModel.mPostSuccess.observe(this){
            ToastUtils.showShort("提交成功")
            ARouter.getInstance().build("/devices/DevicesActivity").navigation()
        }
    }
    override fun getLayoutId(): Int = R.layout.activity_post_devices
}